// -------------------------------------------------
// BlinKit - BlinKit Library
// -------------------------------------------------
//   File Name: qjs_client_rect.cpp
// Description: Bindings for ClientRect
//      Author: Ziming Li
//     Created: 2022-03-15
// -------------------------------------------------
// Copyright (C) 2022 MingYang Software Technology.
// -------------------------------------------------

#include "./qjs_bindings.h"

#include "blinkit/blink/renderer/bindings/core/qjs/qjs.h"
#include "blinkit/blink/renderer/bindings/core/qjs/qjs_c_function_list_entries.h"
#include "blinkit/blink/renderer/bindings/core/script_controller.h"
#include "blinkit/blink/renderer/core/dom/client_rect.h"

namespace BlinKit {
namespace qjs {

/**
 * https://developer.mozilla.org/en-US/docs/Web/API/DOMRect
 */

static JSValue GetBottom(JSContext *ctx, JSValueConst thisVal)
{
    ClientRect *rect = ScriptWrappable::Cast<ClientRect>(thisVal);
    return JS_NewFloat64(ctx, rect->bottom());
}

static JSValue GetHeight(JSContext *ctx, JSValueConst thisVal)
{
    ClientRect *rect = ScriptWrappable::Cast<ClientRect>(thisVal);
    return JS_NewFloat64(ctx, rect->height());
}

static JSValue GetLeft(JSContext *ctx, JSValueConst thisVal)
{
    ClientRect *rect = ScriptWrappable::Cast<ClientRect>(thisVal);
    return JS_NewFloat64(ctx, rect->left());
}

static JSValue GetRight(JSContext *ctx, JSValueConst thisVal)
{
    ClientRect *rect = ScriptWrappable::Cast<ClientRect>(thisVal);
    return JS_NewFloat64(ctx, rect->right());
}

static JSValue GetTop(JSContext *ctx, JSValueConst thisVal)
{
    ClientRect *rect = ScriptWrappable::Cast<ClientRect>(thisVal);
    return JS_NewFloat64(ctx, rect->top());
}

static JSValue GetWidth(JSContext *ctx, JSValueConst thisVal)
{
    ClientRect *rect = ScriptWrappable::Cast<ClientRect>(thisVal);
    return JS_NewFloat64(ctx, rect->width());
}

JSValue CreateClientRectPrototype(JSContext *ctx)
{
    static const JSCFunctionListEntry Funcs[] = {
        CGetSetEntry("bottom", GetBottom, nullptr),
        CGetSetEntry("height", GetHeight, nullptr),
        CGetSetEntry("left", GetLeft, nullptr),
        CGetSetEntry("right", GetRight, nullptr),
        CGetSetEntry("top", GetTop, nullptr),
        CGetSetEntry("width", GetWidth, nullptr)
    };

    JSValue ret = JS_NewObject(ctx);
    JS_SetPropertyFunctionList(ctx, ret, Funcs, std::size(Funcs));
    return ret;
}

} // namespace qjs
} // namespace BlinKit
